#!/bin/sh
LOG=/tmp/susan-postinstall.log
exec > $LOG 2>&1

date
echo "Postinstall. Parameter: $*"

cd @CMAKE_INSTALL_PREFIX@
echo "doing cp -v @libdir@/com.susan.susan-fd.plist $3/Library/LaunchDaemons/com.susan.susan-fd.plist "
cp -v "@scriptdir@/com.susan.susan-fd.plist" "$3/Library/LaunchDaemons/com.susan.susan-fd.plist"

if [ ! -f "@confdir@/susan-fd.conf" -a ! -d "@confdir@/susan-fd.d" ]; then
  echo "migrating config of previous version"
  found_cfg=0
  for cfgdir in \
    /usr/local/susan-19.*/etc/susan \
    /usr/local/susan-18.*/etc/susan \
    /usr/local/etc/susan; do
    if [ -f "${cfgdir}/susan-fd.conf" ]; then
      mkdir -p "@confdir@"
      cp -av "${cfgdir}/susan-fd.conf" "@confdir@"
      found_cfg=1
    fi
    if [ -d "${cfgdir}/susan-fd.d" ]; then
      mkdir -p "@confdir@"
      cp -av "${cfgdir}/susan-fd.d" "@confdir@"
      found_cfg=1
    fi
    if [ -f "${cfgdir}/bconsole.conf" ]; then
      mkdir -p "@confdir@"
      cp -av "${cfgdir}/bconsole.conf" "@confdir@"
      found_cfg=1
    fi
    if [ $found_cfg -gt 0 ]; then
      break
    fi
  done
else
  echo "configuration already in place. Skipping migration"
fi

@scriptdir@/susan-config deploy_config "@configtemplatedir@" "@confdir@" susan-fd
@scriptdir@/susan-config deploy_config "@configtemplatedir@" "@confdir@" bconsole

# Load startup item
/bin/launchctl load -w "$3/Library/LaunchDaemons/com.susan.susan-fd.plist"

echo "finished"
